<script setup lang="ts">
import { ref } from "vue";
import { onLoad } from "@dcloudio/uni-app";
import { useQuery } from "@/hooks/init";
import { useShare } from "@/hooks/share";
import { Account } from "@/utils/account";
import { Token } from "@/utils/token";

onLoad((options) => {
  useQuery<{ url: string; share?: string }>(
    options as MiniApp.AppLaunchOption,
    (query) => {
      let url = query.url;
      // 三毛h5增加cpl、auth
      if (url.includes("sanmaoyou.com")) {
        // 拼接 cpl
        const cpl = encodeURIComponent(Token.cpl);
        url += `${url.includes("?") ? "&" : "?"}cpl=${cpl}`;

        // 已登录拼接 auth
        if (Account.isLogin) {
          const auth = encodeURIComponent(Token.auth);
          url += `&auth=${auth}`;
        }
        webUrl.value = url;
      } else {
        webUrl.value = url;
      }

      // 默认关闭分享
      if (query.share !== "1") uni.hideShareMenu({ hideShareItems: [] });

      shareOption.data = {
        url: encodeURIComponent(query.url),
        share: query.share || "",
      };
    }
  );
});

const webUrl = ref("");

const { onShareAppMessage, shareOption } = useShare("/pages/web/index");
</script>

<template>
  <web-view v-if="webUrl" :src="webUrl" />
</template>
